import 'dart:async';

import 'package:base_lib/common/bloc/base_bloc.dart';
import 'package:base_lib/index.dart';
import 'package:base_lib/utils/text_util.dart';
import 'package:doctor_login_lib/cache/userinfo_cache.dart';
import 'package:doctor_login_lib/component.dart';
import 'package:doctor_login_lib/login/entity/doctor_login_entity.dart'
    as LoginBean;
import 'package:doctor_user_lib/repository/https.dart';
import 'package:doctor_user_lib/setting/setting_entity.dart';

class SettingBloc extends BaseBloc {
  StreamController<List<SettingEntity>> settingSubject = StreamController();

  List<SettingEntity> settingList = List<SettingEntity>();
  LoginBean.DoctorUser _userInfo = LoginBean.DoctorUser();
  @override
  void initState() async {
    super.initState();
    _userInfo = await UserInfoCache.getInstance().get();
    settingList = _fetchListDatas();
    settingSubject.add(settingList);
  }

  @override
  void dispose() {
    settingSubject?.close();
  }

  // 网路请求--退出登录
  logOut(context) async {
    var params = Map<String, dynamic>()
      ..["personID"] = _userInfo.keyID
      ..["token"] = SPUtil.getString("token");
    var result = await DoctorUserHttp.logOut(params);
    if (result.isSuccess) {
      SPUtil.remove("token");
      Routers.navigateTo(context, DoctorLoginComponent.loginPage,
          clearStack: true);
    } else {}
  }

  _fetchListDatas() {
    var nameEntity = SettingEntity()
      ..title = "个人资料"
      ..detail = _userInfo.doctorName
      ..isArrows = true
      ..isBottomLine = true
      ..isShow = true;
    var phoneEntity = SettingEntity()
      ..title = "手机绑定"
      ..detail = TextUtil.hideNumber(_userInfo.phone)
      ..isArrows = false
      ..isBottomLine = true
      ..isShow = true;
    var aboutEntity = SettingEntity()
      ..title = "关于我们"
      ..detail = "版本1.0.0"
      ..isArrows = false
      ..isBottomLine = true
      ..isShow = true;
    var agreementEntity = SettingEntity()
      ..title = "注册协议"
      ..detail = ""
      ..isArrows = true
      ..isBottomLine = true
      ..isShow = true;
    var policyEntity = SettingEntity()
      ..title = "隐私政策"
      ..detail = ""
      ..isArrows = true
      ..isBottomLine = true
      ..isShow = true;

    List<SettingEntity> tempList = [
      nameEntity,
      phoneEntity,
      aboutEntity,
      agreementEntity,
      policyEntity
    ];
    return tempList.where((element) => element.isShow == true).toList();
  }
}
